import common from '/@/common/common'
/* eslint-disable no-unused-vars */
// 接口类型和方法
interface BaseType{
    readonly EQUAL: number
    readonly UNEQUAL: number
    readonly LIKE: number
    readonly NOT_LIKE: number
    delRowbyCol(data: Array<Object>, colName: string, colValue: any, type: number):Array<Object>
}



class JsonHandle implements BaseType{
    EQUAL: number
    UNEQUAL: number
    LIKE: number
    NOT_LIKE: number
    // 符合某个字段的条件类型，就将这些对象从json数组中删除
    // 比如10行的json数组，有3行的某个A字段为0，就把这3行删除
    //匹配方式有4种：等于、不等于、包含、不包含
    delRowbyCol = (data: Array<Object>, colName: string, colValue: any, type: number) =>{
        for (let i = data.length - 1; i >= 0; i--) {
            let delFlag = false
            switch (type) {
                case this.EQUAL:
                    if (common.objectUnavailable(data[i][colName])) {
                        delFlag = false
                    } else {
                        data[i][colName] === colValue?delFlag = true:delFlag = false
                    }
                    break;
                case this.UNEQUAL:
                    if (common.objectUnavailable(data[i][colName])) {
                        delFlag = true
                    } else {
                        data[i][colName] != colValue?delFlag = true:delFlag = false
                    }
                    break;
                case this.LIKE:
                    if (common.objectUnavailable(data[i][colName])) {
                        delFlag = false
                    } else {
                        data[i][colName].indexOf(colValue) >= 0 ?delFlag = true:delFlag = false
                    }
                    break;
                case this.NOT_LIKE:
                    if (common.objectUnavailable(data[i][colName])) {
                        delFlag = true
                    } else {
                        data[i][colName].indexOf(colValue) < 0 ?delFlag = true:delFlag = false
                    }
                    break;
                default:
                    if (common.objectUnavailable(data[i][colName])) {
                        delFlag = false
                    } else {
                        data[i][colName] === colValue?delFlag = true:delFlag = false
                    }
            }
            if (delFlag) {
                data.splice(i, 1);
            }
        }
        return data
    }
}

const json = new JsonHandle()
json.EQUAL = 1
json.UNEQUAL = 2
json.LIKE = 3
json.NOT_LIKE = 4

export default json;